        TITL DAI FIRMWARE D3F4-D61F  V1.1
        ORG   :D3F4
*
*
*
********************
* READ FILE HEADER *
********************
*
* Locates a file on tape and reads leader.
* Exit: Interrupts are disabled. BCDEHL preserved.
*
RHDR    CALL  SNDDI     Disable sound interrupt
        CALL  RLEAD     Find sync pattern
        CALL  RBYTE     Read flag type byte
        JC    RHDR      Again if reading error
        CPI   :55
        JNZ   RHDR      Again if not flag byte
        CALL  RBYTE     Read file type byte
        JC    RHDR      Again if reading error
        RET 
*
*********************
* WRITE FILE LEADER *
*********************
*
* Writes a leader for program or data block on tape.
* Disables interrupts which could cause problems.
*
* Entry: at WHDR:  Entry if not during run program.
*        at WHD20: If during run of program.
* Exit:  BCDEHL preserved.
*
WHDR    CALL  PMSGR     Print 'SET RECORD, START
        DBL   MSG15     TAPE, TYPE SPACE'
        CALL  WPT       Wait for spacebar pressed
WHD20   CALL  CASST     Switch on cassette motors
        DI              Disable interrupts
        NOP 
        NOP 
        CALL  WLEAD     Write leader
        MVI   A,:55     Get flag byte
        JMP   WBYTE     Write flag byte
*
**************
* (Not used) *
**************
*
MPT27   CALL  CWBLK     Write block on tape
        NOP 
        NOP 
*
**********************
* WRITE FILE TRAILER *
**********************
*
* Write a trailer for program or datablock.
*
* Entry: Length of trailer in C.
* Exit:  A=0, BCDEHL preserved.
*
*WTRL
CWCLOS  CALL  WTRLX     Write trailer bytes
        EI              Enable interrupts
        JMP   CASSP     Stop cassette motors
*
*************************
* START CASSETTE MOTORS *
*************************
*
* Turns on motor of selected cassettedeck and
* waits 665 msec.
*
* Exit: All registers preserved.
*
CASST   PUSH  PSW
        LDA   POROM     Load POROM
        ORI   :30       Disable cassette motors
        PUSH  H
        LXI   H,CASSL   Addr CASSL
        XRA   M         Get selected cassette
        POP   H
        STA   POROM     Remember POROM
        STA   PORO      Switch cassette motor on
        CALL  DELAY     Delay
        POP   PSW
        RET 
*
************************
* STOP CASSETTE MOTORS *
************************
*
* Switches off cassettemotors.
*
* Exit: All registers preserved.
*
*CRCLOS
CASSP   PUSH  PSW
        LDA   POROM     Load POROM
        ORI   :30       Disable cassette motors
        STA   POROM     Remember POROM
        STA   PORO      Switch cassette motors off
        POP   PSW
        RET 
*
************
* READ BIT *
************
*
* Reads one bit from tape.
*
* Entry: Address input port in HL. Input low state.
* Exit:  CY=0: sign bit of A is bit read.
*        CY=1: reading error.
*        BHL preserved.
*
RBIT    XRA   A
        MOV   D,A
        MOV   B,A
        MOV   C,A
 REM
* 1st impulse:
 REM
RBT10   DCR   B
        JZ    RBT90     Too long low
        ORA   M
        JP    RBT10     Wait for high
RBT30   DCR   C
        JZ    RBT90     Too long high
        DCR   D
        ANA   M
        JM    RBT30     Wait low
        LXI   B,:0000
 REM
* 2nd impulse:
 REM
RBT40   DCR   B
        JZ    RBT90     Too long low
        ORA   M
        JP    RBT40     Wait high again
RBT50   DCR   C
        JZ    RBT90     Too long high
        INR   D
        ANA   M
        JM    RBT50     Wait low
        MOV   A,D
        RET 
 REM
* If error:
 REM
RBT90   STC             Set CY if error
        RET 
*
***************
* READ LEADER *
***************
*
* Finds a section of leader on the tape.
*
* Entry: No conditions.
* Exit:  BCDEHL preserved, interrupts disabled.
*
RLEAD   PUSH  B
        PUSH  D
        PUSH  H
        MVI   B,:28     Estimate of impulse length
        LXI   H,PORI    Address input port
RDL05   MVI   A,:FF
RDL10   EI              Enable interrupts
        NOP             (here cursor flashes)
        DI              Disable interrupts
        ANA   M
        JM    RDL10     Wait low
        MOV   C,B       Estimated length in C
        MVI   D,:14     Needs this many cycles for
 REM synchronisation. Must be
 REM more than trailer length.
RDL30   MVI   E,:00
        XRA   A
RDL40   DCR   E
        JZ    RDL05     Too long low; start again
        ORA   M
        JP    RDL40     Wait high
        MVI   B,:00
RDL50   INR   B
        JZ    RDL05     Too long high; start again
        ANA   M
        JM    RDL50     Wait low
        MOV   A,B
        SUB   C         Compare impulse length
 REM with estimate
        JP    RDL60
        CMA             )
        INR   A         ) 2-complement if <0
RDL60   MOV   E,A       Store difference in E
        MOV   A,C       Calculate margin
        ANI   :F0
        RAR 
        RAR 
        RAR             Margin: 1/8th of estimate
        CMP   E         Compare with difference
        JC    RDL70     Not within margin
 REM
* If sync archieved:
 REM
        DCR   D
        JNZ   RDL30     Next impulse until D=0
        INR   D
        JMP   RDL30     Next impulse until out
 REM of margin
 REM
* If out of margin:
 REM
RDL70   DCR   D
        JNZ   RDL05     Not synchronised; again
        XRA   A
RDL80   ORA   M
        JP    RDL80     Wait high
RDL90   ANA   M
        JM    RDL90     Wait low
        POP   H
        POP   D
        POP   B
        RET 
*
*************
* READ BYTE *
*************
*
* Reads one byte from tape.
*
* Entry: No conditions.
* Exit:  CY=0: Byte read in A.
*        CY=1: Some error.
*        BCDEHL preserved.
*
RBYTE   PUSH  B
        PUSH  D
        PUSH  H
        LXI   H,PORI    Address input port
        MVI   E,:FE
RBY10   CALL  RBIT      Read bit
        JC    RBY20     If reading error; CY=1
        RAL 
        MOV   A,E
        RAL 
        MOV   E,A       Shift bit into E
        JC    RBY10     Next bit
RBY20   POP   H         8 bits read, no error
        POP   D
        POP   B
        RET 
*
****************
* WRITE LEADER *
****************
*
* Writes a leader on the tape. From WLD10 also used
* to write a trailer.
*
* Entry: No conditions.
* Exit:  A=0, BCDEHL preserved.
*
WLEAD   NOP 
        PUSH  B
        PUSH  H
        LHLD  TAPSL     Get leader impulse length
        LXI   B,:07E8   Period for synchr.
WLD10   CALL  WBIT      Write bit
        DCX   B
        MOV   A,B
        ORA   C
        JNZ   WLD10     Write many bits
        LHLD  TAPSD     Get impulse length data bit
        CALL  WBIT      Write a data '1' bit to end
        POP   H
        POP   B
        NOP 
        RET 
*
**************
* WRITE BYTE *
**************
*
* Write a byte to tape.
*
* Entry: Byte to be written in A.
* Exit:  All registers preserved.
*
WBYTE   PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        LHLD  TAPSD     Get impulse length bit '1'
        MOV   E,H
        MOV   D,L       DE: inpulse length bit '0'
        MVI   B,:08     8 bits to write
WBY10   RAL             Set/reset CY for kind of bit
        CC    WBIT      Write data '1' bit
        XCHG
        CNC   WBIT      Write data '0' bit
        XCHG
        DCR   B
        JNZ   WBY10     Next bit
        JMP   LC188     Pop all, ret
*
*************
* WRITE BIT *
*************
*
* Write 2 impulses on tape, one long, one short.
*
* Entry: H: Half count first cycle.
*        L: Half count second cycle.
* Exit:  All registers preserved.
*
WBIT    PUSH  PSW
        PUSH  D
        PUSH  H
        MOV   L,H
        LXI   D,PORO    Address output port
        CALL  WCYC      Write 1st impulse
        POP   H
        PUSH  H
        MOV   H,L
        MOV   A,L
        SUI   :08       Allow for return to WBYTE
        MOV   L,A
        CALL  WCYC      Write 2nd impulse
        POP   H
        POP   D
        POP   PSW
        RET 
*
***************
* WRITE CYCLE *
***************
*
* Writes one impulse (hi/lo) on tape. Two cycles
* are required for one bit.
*
* Entry: DE: Address output port.
*        HL: Impulse length constants.
* Exit:  HL = 0. BCDE preserved.
*
WCYC    LDA   POROM     POROM in A
        ORI   :01       lsb = 1
        STAX  D         Output port is made '1'
WCY10   DCR   H
        JNZ   WCY10     Write '1' until H=0
        DCR   L
        DCR   L
        DCR   L         Allow for return to WBIT
        DCR   A
        STAX  D         Output port is made '0'
WCY20   DCR   L
        JNZ   WCY20     Write '0' until L=0
        RET 
*
**********************
* WRITE TRAILER BITS *
**********************
*
* Writes trailer bits after a block on tape.
*
* Entry: Number of trailer bits in C.
* Exit:  A=0, other registers preserved.
*        F corrupted.
*
WTRLX   PUSH  B
        PUSH  H
        LHLD  TAPST     Trailer impulse length
        MVI   B,:00
        JMP   WLD10     Write trailer bits
        STA   OTSW      ] Output to screen
        JMP   IPL30     ] Ignore line if break
 REM pressed
*
********************************
* INITIALISE KEYBOARD POINTERS *
********************************
*
* Set all keyboard pointers to default values.
*
* Entry: Address ASCII-table in HL (3E8C5).
* Exit:  BCDE preserved.
*
*KLIRS
KBINIT  SHLD  KBTPT     Load pointer ASCII-table
KLIRP   XRA   A
        STA   KNSCAN    Allow complete scan routine
        STA   SHFLCK    CTRL not pressed
        LXI   H,KLIND
        SHLD  KLIIN     Set KLIIN ) Ignore
        SHLD  KLIOU     Set KLIOU ) previous inputs
        CMA 
        STA   KBRFL     BREAK pointer = FF
        RET 
*
**************************************
* KEYBOARD INTERRUPT SERVICE (RST 6) *
**************************************
*
* Current interrupt mask is saved. Only stack and
* clock interrupts are allowed. Keyboard timer 4
* is re-loaded.
* KBXCT is counted down: abort if not 0.
* Else: scan keyboard and store result.
*
* Entry: None.
* Exit:  All registers + int. mask preserved.
*
KBINT   PUSH  PSW
        PUSH  B
        PUSH  D
        LDA   CHOPRT
        PUSH  PSW       Preserve current int. mask
        MVI   A,:84     )
        STA   TICC8     ) Allow stack and clock
        STA   CHOPRT    ) interrupts only
        EI  
        CALL  KBIS      Reload keyboard timer
        LXI   H,KBXCT
        DCR   M         Decr. keyb.scan time count
        JNZ   INTRM     No scanning if <>0
 REM
* if KBXCT = 0:
 REM
        MVI   M,:02     Set keyb. scan time counter
        CALL  KBSCAN    Scan keyboard, store result
        JMP   INTRM     Restore int.mask; ret.
*
*******************************
* SCAN KEYBOARD, STORE RESULT *
*******************************
*
* Exit: All registers corrupted.
*
KBSCAN  LXI   D,TICCTX  Input port from keyboard
        LXI   H,TICC7   Output port to keyboard
        LXI   B,KBRFL   BREAK pointer
        DI  
        MVI   M,:40     Scan row 6
        LDAX  D         and get result
        EI  
        ADD   A         Check for BREAK pressed
        JM    KEB90     If BREAK pressed
        CALL  MPT28     Update BREAK pointer
        LDA   KNSCAN    Get BREAK pointer
        ORA   A         Scan for BREAK only?
        JNZ   KEB81     Then abort
 REM
* Scan all rows and store result in MAP1:
 REM
        LXI   B,KBTPUT  MAP1 for currently
 REM pressed key
        PUSH  B         Preserve MAP1 addr
        INR   A         Determine row
KEB10   PUSH  PSW
        DI  
        MOV   M,A       Scan row
        LDAX  D         Get result
        EI  
        STAX  B         Store result in MAP1
        INX   B
        POP   PSW
        ADD   A         Determine next row
        JNC   KEB10     Scan next row if not ready
 REM
* REPT handling:
 REM
        LDA   KBTEND
        ANI   :20       Check if REPT pressed
        MOV   B,A       Store result
        LXI   H,RPCNT   Addr. REPT counter
        MOV   A,M       Get contents
        MVI   M,:01     Update it for immediate scan
        JZ    KEB40     If REPT not pressed
        DCR   A         Else
        MOV   M,A       Decr. REPT counter
        JNZ   KEB80     If <>0, abort scan
        MVI   M,:02     Else RPCNT=2
        MVI   B,:FF     Set B=FF for REPT pressed
 REM
* ASCII-value of key pressed into KLIND:
 REM
KEB40   POP   H         Get addr MAP1
        PUSH  H         Save addr. MAP1
        LXI   D,KBTCNT  Get addr. MAP2
        MVI   C,:00
KEB50   MOV   A,M       Get result scan current
 REM row in A
        DCR   B
        INR   B         REPT pressed ?
        JNZ   KEB60     If REPT pressed
        XCHG
        XRA   M         ) Check if new input
        XCHG            )
        ANA   M         )
KEB60   ORA   A         )
        CNZ   TKEY      If new: Get ASCII-code
 REM and store it in KLIND
        INX   D
        INX   H         Next row
        INR   C
        MOV   A,C
        CPI   :08       All rows checked?
        JNZ   KEB50     Next row if not
KEB70   POP   D         Get MAP1 addr in DE
        PUSH  D
        MOV   B,H       ) Get MAP2 addr in HL
        MOV   C,L       )
        CALL  MOVE      Transfer (MAP1) into MAP2
KEB80   POP   D         Scrap
KEB81   RET 
 REM
* if BREAK pressed:
 REM
KEB90   PUSH  B         Save Breakpntr
        CALL  SNDINI    All sound off
        POP   B
        NOP 
        CALL  CASSP     Stop cassette motors
        LDAX  B         Get KBRFL
        INR   A
        JZ    KEB81     If KBRFL=FF: break acknow-
 REM ledged already
        STAX  B         Else: store new KBRFL
        CPI   :20
        JNZ   KEB81     If new KBRFL<>20: wait for
 REM soft-break to be accepted
        CALL  KLIRP     Else: init keyb. pointers
        JMP   RSTART    Print 'BREAK', return to
 REM monitor
*
*
        UNL
SNDDI   EQU   :D98F
PMSGR   EQU   :DAFF
MSG15   EQU   :DB9C
WPT     EQU   :D7CB
CWBLK   EQU   :D2F1
POROM   EQU   :0040
CASSL   EQU   :013D
PORO    EQU   :FD06
DELAY   EQU   :DE41
PORI    EQU   :FD00
TAPSL   EQU   :02E6
TAPSD   EQU   :02E8
LC188   EQU   :CB56
TAPST   EQU   :02EA
OTSW    EQU   :0131
IPL30   EQU   :DD49
KBTPT   EQU   :02A7
KNSCAN  EQU   :02B9
SHFLCK  EQU   :02C3
KLIND   EQU   :02BA
KLIIN   EQU   :02BE
KLIOU   EQU   :02C0
KBRFL   EQU   :02C4
CHOPRT  EQU   :005F
TICC8   EQU   :FFF8
KBIS    EQU   :D99D
KBXCT   EQU   :01C1
INTRM   EQU   :D9CD
TICCTX  EQU   :FFF1
TICC7   EQU   :FFF7
MPT28   EQU   :D750
KBTPUT  EQU   :02A9
KBTEND  EQU   :02AF
RPCNT   EQU   :02C2
KBTCNT  EQU   :02B1
TKEY    EQU   :D632
MOVE    EQU   :DE4F
SNDINI  EQU   :D8A6
RSTART  EQU   :C80C
        LST
        END 
